﻿using ChuanYe.Core.Utils;
using ChuanYe.CountyFireManagement.EF.MySQL;
using ChuanYe.CountyFireManagement.EF.MySQL.Models;
using ChuanYe.CountyFireManagement.Model;
using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
namespace ChuanYe.CountyFireManagement.BLL
{
    public class UserBLL
    {
        #region 构造单例
        private UserBLL() { }
        private static UserBLL _instance;
        public static UserBLL Instance
        {
            get
            {
                return _instance ?? (_instance = new UserBLL());
            }
        }
        #endregion

        public ResultDto<dynamic,dynamic,dynamic,dynamic> Login(string accountName, string password)
        { 
            ResultDto<dynamic,dynamic, dynamic,dynamic> result = new ResultDto<dynamic,dynamic, dynamic,dynamic>();
            using (CountyFireManagementContext ef = new CountyFireManagementContext())
            {
                var exist= ef.Userinfo.Where(p=>p.AccountName==accountName).FirstOrDefault();
                if (exist == null)
                {
                    result.Status = -1;
                    result.Message = "AccountName Non-existent";
                }
                else if (exist.Password != password)
                {
                    result.Status = -2;
                    result.Message = "Password Incorrect";
                }
                else
                {
                    var powerIds = ef.Rolepower.Where(p => p.RoleId == exist.RoleId).Select(s=>s.PowerId).ToList();
                    var powers=ef.Power.Where(p => powerIds.Contains(p.PowerId)).ToList();
                    exist.Password = string.Empty;
                    var existRole= ef.Role.Where(p => p.RoleId == exist.RoleId).FirstOrDefault();
                    result.FirstParam = exist;
                    result.SecondParam = powers;
                    result.ThirdParam = existRole;
                }
            }
            return result;
        }

        public ResultDto<dynamic, dynamic> GetPowerByPage(string powerKey, string powerName, RequestPageBase page)
        {
            ResultDto<dynamic, dynamic> result = new ResultDto<dynamic, dynamic>();
            var sql = AppSetting.GetConfig("Sql:BasicSelect");
            sql = string.Format(sql, "power");
            using (CountyFireManagementContext ef = new CountyFireManagementContext())
            {
                string paramsSql = string.Empty;
                if (!string.IsNullOrEmpty(powerKey))
                {
                    paramsSql += powerKey.KeywordSql("PowerKey");
                }
                if (!string.IsNullOrEmpty(powerName))
                {
                    paramsSql += powerName.KeywordSql("PowerName");
                }
                paramsSql += page.OrderByPattern == 0 ? "Sort".OrderByDescending() : "Sort".OrderBy();
                paramsSql += page.Limit();
                sql += paramsSql;
                var countSql = sql.CountSql();
                var list = ef.Database.SqlQuery<Power>(sql);
                result.FirstParam = list;
                result.SecondParam = ef.Database.SqlQuery(countSql).Rows[0][0];
            }
            return result;
        }

        public int RemovePower(int powerId)
        {
            using (CountyFireManagementContext ef = new CountyFireManagementContext())
            {
                var existRel = ef.Rolepower.Where(p => p.PowerId == powerId).FirstOrDefault();
                if (existRel != null)
                {
                    return -2;
                }
                else
                {
                    var exist = ef.Power.Where(p => p.PowerId == powerId).FirstOrDefault();
                    if (exist == null)
                    {
                        return -1;
                    }
                    ef.Power.Add(exist);
                    ef.Entry<Power>(exist).State = EntityState.Deleted;
                    return ef.SaveChanges();
                }
            }

        }

        public int UpdatePower(Power info)
        {
            using (CountyFireManagementContext ef = new CountyFireManagementContext())
            {
                var exist = ef.Power.Where(p => p.PowerId == info.PowerId).FirstOrDefault();
                if (exist == null)
                {
                    return -1;
                }
                else
                {
                    int isUpdate= ef.Power.Where(p => p.PowerKey == info.PowerKey && p.PowerId!=info.PowerId).ToList().Count();
                    if (isUpdate != 0)
                    {
                        return -2;
                    }
                    exist.PowerKey = info.PowerKey;
                    exist.PowerName = info.PowerName;
                    exist.Sort = info.Sort;
                    exist.ParentId = info.ParentId;
                    ef.Power.Add(exist);
                    ef.Entry<Power>(exist).State = EntityState.Modified;
                    return ef.SaveChanges();
                }
            }
        }

        public List<AddressUserInfoModel> GetAddressUserInfo(int depId,string addressCounty,string addressTown,string asddressVillage) 
        {
            using (CountyFireManagementContext ef = new CountyFireManagementContext())
            {
                var nowDateTime = DateTime.Now;
                string sql = " select UserID,RealName,Phone from userinfo where 1=1  ";
                if (!string.IsNullOrEmpty(addressCounty))
                {
                    sql += $" and AddressCounty='{addressCounty}' ";
                }

                if (!string.IsNullOrEmpty(addressTown))
                {
                    sql += $" and AddressTown='{addressTown}' ";
                }

                if (!string.IsNullOrEmpty(asddressVillage))
                {
                    sql += $" and AddressVillage='{asddressVillage}' ";
                }

                if (depId > 0)
                {
                    sql += $" and DepId='{depId}' ";
                }

                sql += " order by Sort desc ";
                var list = ef.Database.SqlQuery<AddressUserInfoModel>(sql);
                return list;
            }
        }
        public int RemoveRole(int roleId)
        {
            using (CountyFireManagementContext ef = new CountyFireManagementContext())
            {
                var exist = ef.Role.Where(p => p.RoleId == roleId).FirstOrDefault();
                if (exist == null)
                {
                    return -1;
                }
                var existUser = ef.Userinfo.Where(p => p.RoleId == roleId).FirstOrDefault();
                if (existUser != null)
                {
                    return -2;
                }
                var existRel = ef.Rolepower.Where(p => p.RoleId == roleId).FirstOrDefault();
                if (existRel != null)
                {
                    return -3;
                }
                ef.Role.Add(exist);
                ef.Entry<Role>(exist).State = EntityState.Deleted;
                return ef.SaveChanges();
            }
        }

        public List<Department> GetDepartment()
        {
            using (CountyFireManagementContext ef = new CountyFireManagementContext())
            {
                return ef.Department.OrderBy(o => o.Sort).ToList();
            }
        }

        public List<InformUserModel> GetInformUserInfo(int userId,int fireId)
        {
            var sql = AppSetting.GetConfig("Sql:UserBLL.GetInformUser");
            using (CountyFireManagementContext ef = new CountyFireManagementContext())
            {
                string paramsSql = string.Empty;
                if (userId > 0)
                {

                    var existUser = ef.Userinfo.Where(p => p.UserId == userId).FirstOrDefault();
                    if (existUser != null)
                    {
                        paramsSql += $" and AddressCounty ='{existUser.AddressCounty}' ";
                        paramsSql += $" and AddressTown !='{existUser.AddressTown}' ";
                        paramsSql += $" and AddressTown !='NULL'  ";
                    }
                }
                sql = string.Format(sql, paramsSql);
                var list = ef.Database.SqlQuery<InformUserModel>(sql);

                //排除火灾所在的位置
                if (fireId > 0)
                {
                    var existFireInfo = ef.Fireinfo.Where(p => p.FireId == fireId).FirstOrDefault();
                    if (existFireInfo != null)
                    {
                        list = list.Where(p => p.AddressTown != existFireInfo.AddressTown 
                                            && p.AddressVillage != existFireInfo.AddressVillage).ToList();
                    }
                }
                return list;
            }
        }

        public int UpdateRole(Role info)
        {
            using (CountyFireManagementContext ef = new CountyFireManagementContext())
            {
                var exist = ef.Role.Where(p => p.RoleId == info.RoleId).FirstOrDefault();
                if (exist == null)
                {
                    return -1;
                }
                else
                {
                    int isUpdate = ef.Role.Where(p => p.RoleId != info.RoleId && p.RoleName == info.RoleName).ToList().Count();
                    if (isUpdate != 0)
                    {
                        return -2;
                    }
                    exist.RoleName = info.RoleName;
                    exist.Description = info.Description;
                    exist.Enabled = info.Enabled;
                    exist.RoleLevelId = info.RoleLevelId;
                    ef.Role.Add(exist);
                    ef.Entry<Role>(exist).State = EntityState.Modified;
                    return ef.SaveChanges();
                }
            }
        }

        public int RemoveDepartment(int depId)
        {
            using (CountyFireManagementContext ef = new CountyFireManagementContext())
            {
                var exist = ef.Department.Where(p => p.DepId == depId).FirstOrDefault();
                if (exist == null)
                {
                    return -1;
                }
                ef.Department.Add(exist);
                ef.Entry<Department>(exist).State = EntityState.Deleted;
                return ef.SaveChanges();

            }
        }

        public ResultDto<dynamic, dynamic> GetRolePowerByPage(RequestPageBase page)
        {
            ResultDto<dynamic, dynamic> result = new ResultDto<dynamic, dynamic>();
            using (CountyFireManagementContext ef = new CountyFireManagementContext())
            {
                var linq = from a in ef.Rolepower select a;
                var linq2 = from a in linq.ToList()
                            join b in ef.Role on a.RoleId equals b.RoleId
                            into c
                            let d = c.FirstOrDefault()
                            join e in ef.Power on a.PowerId equals e.PowerId
                            into f
                            let g = f.FirstOrDefault()
                            select new
                            {
                                a.RolePowerId,
                                a.PowerId,
                                a.RoleId,
                                a.CreateTime,
                                d.RoleName,
                                g.PowerName,
                                g.PowerKey
                            };
                if (!string.IsNullOrEmpty(page.Keyword))
                {
                    linq2 = linq2.Where(p => p.PowerKey.Contains(page.Keyword)
                                         || p.PowerName.Contains(page.Keyword)
                                         || p.RoleName.Contains(page.Keyword));
                }
                int startRow = page.PageSize * (page.PageIndex - 1);
                var list = linq2.OrderBy(o => o.CreateTime).ThenByDescending(t => t.RoleId)
               .Skip(startRow).Take(page.PageSize).ToList();
                result.FirstParam = list;
                result.SecondParam = linq2.Count();
            }
            return result;
        }

        public int UpdateRolePower(Rolepower info)
        {
            using (CountyFireManagementContext ef = new CountyFireManagementContext())
            {
                var exist = ef.Rolepower.Where(p => p.RolePowerId == info.RolePowerId).FirstOrDefault();
                if (exist == null)
                {
                    return -1;
                }
                else
                {
                    int isUpdate = ef.Rolepower.Where(p => p.RolePowerId != info.RolePowerId 
                                                      && p.RoleId == info.RoleId && p.RolePowerId == info.RolePowerId).ToList().Count();
                    if (isUpdate != 0)
                    {
                        return -2;
                    }
                    exist.RoleId = info.RoleId;
                    exist.PowerId = info.PowerId;
                    exist.CreateTime = info.CreateTime;
                    ef.Rolepower.Add(exist);
                    ef.Entry<Rolepower>(exist).State = EntityState.Modified;
                    return ef.SaveChanges();
                }
            }
        }

        public int RemoveRolePower(int rolePowerId)
        {
            using (CountyFireManagementContext ef = new CountyFireManagementContext())
            {
                var exist = ef.Rolepower.Where(p => p.RolePowerId == rolePowerId).FirstOrDefault();
                if (exist != null)
                {
                    ef.Rolepower.Add(exist);
                    ef.Entry<Rolepower>(exist).State = EntityState.Deleted;
                    return ef.SaveChanges();
                }
                else
                {
                    return -1;
                }
            }
        }

        public int AddDepartment(Department info)
        {
            using (CountyFireManagementContext ef = new CountyFireManagementContext())
            {
                if (!info.Sort.HasValue)
                {
                    var maxSortInfo = ef.Department.OrderByDescending(o => o.Sort).FirstOrDefault();
                    if (maxSortInfo != null)
                    {
                        info.Sort = maxSortInfo.Sort.Value + 1;
                    }
                    else
                    {
                        info.Sort = 1;
                    }
                }

                int isAdd = ef.Department.Where(p => p.DepName == info.DepName).ToList().Count();
                if (isAdd != 0)
                {
                    return -2;
                }

                ef.Department.Add(info);
                ef.Entry<Department>(info).State = EntityState.Added;
                return ef.SaveChanges();
            }
        }
        public ResultDto<dynamic, dynamic> GetRoleByPage(string roleName, RequestPageBase page)
        {
            ResultDto<dynamic, dynamic> result = new ResultDto<dynamic, dynamic>();
            var sql = AppSetting.GetConfig("Sql:BasicSelect");
            sql = string.Format(sql, "role");
            using (CountyFireManagementContext ef = new CountyFireManagementContext())
            {
                string paramsSql = string.Empty;
                if (!string.IsNullOrEmpty(roleName))
                {
                    paramsSql +=roleName.KeywordSql("RoleName") ;
                }
                paramsSql += page.OrderByPattern == 0 ? "Enabled".OrderByDescending() : "Enabled".OrderBy();
                paramsSql += page.Limit();
                sql += paramsSql;
                var countSql = sql.CountSql();
                var list = ef.Database.SqlQuery<Role>(sql);
                result.FirstParam = list;
                result.SecondParam = ef.Database.SqlQuery(countSql).Rows[0][0];
            }
            return result;
        }

        public int UpdateDepartment(Department info)
        {
            using (CountyFireManagementContext ef = new CountyFireManagementContext())
            {
                var exist = ef.Department.Where(p => p.DepId == info.DepId).FirstOrDefault();
                if (exist == null)
                {
                    return -1;
                }
                else
                {
                    int isUpdate = ef.Department.Where(p => p.DepId != info.DepId && p.DepName == info.DepName).ToList().Count();
                    if (isUpdate != 0)
                    {
                        return -2;
                    }
                    exist.DepName = info.DepName;
                    exist.Sort = info.Sort;
                    exist.Remarks = info.Remarks;
                    ef.Department.Add(exist);
                    ef.Entry<Department>(exist).State = EntityState.Modified;
                    return ef.SaveChanges();
                }
            }
        }

        public ResultDto<dynamic, dynamic> GetDepartmentByPage(RequestPageBase page)
        {
            ResultDto<dynamic, dynamic> result = new ResultDto<dynamic, dynamic>();
            var sql = AppSetting.GetConfig("Sql:BasicSelect");
            sql = string.Format(sql, "department");
            using (CountyFireManagementContext ef = new CountyFireManagementContext())
            {
                string paramsSql = string.Empty;
                if (!string.IsNullOrEmpty(page.Keyword))
                {
                    paramsSql += page.Keyword.KeywordSql("DepName");
                }
                paramsSql += page.OrderByPattern == 0 ? "Sort".OrderByDescending() : "Sort".OrderBy();
                paramsSql += page.Limit();
                sql += paramsSql;
                var countSql = sql.CountSql();
                var list = ef.Database.SqlQuery<Department>(sql);
                result.FirstParam = list;
                result.SecondParam = ef.Database.SqlQuery(countSql).Rows[0][0];
            }
            return result;
        }

        public List<Position> GetPosition()
        {
            using (CountyFireManagementContext ef = new CountyFireManagementContext())
            {
                return ef.Position.OrderBy(o => o.Sort).ToList();
            }
        }

        public int RemovePosition(int positionId)
        {
            using (CountyFireManagementContext ef = new CountyFireManagementContext())
            {
                var exist = ef.Position.Where(p => p.PositionId == positionId).FirstOrDefault();
                if (exist != null)
                {
                    ef.Position.Add(exist);
                    ef.Entry<Position>(exist).State = EntityState.Deleted;
                    return ef.SaveChanges();
                }
                else
                {
                    return 0;
                }
            }
        }

        public int AddRole(Role info)
        {
            using (CountyFireManagementContext ef = new CountyFireManagementContext())
            {
                int isAdd = ef.Role.Where(p => p.RoleName == info.RoleName).ToList().Count();
                if (isAdd != 0)
                {
                    return -2;
                }
                ef.Role.Add(info);
                ef.Entry<Role>(info).State = EntityState.Added;
                return ef.SaveChanges();
            }
        }

        public List<Role> GetRole()
        {
            using (CountyFireManagementContext ef = new CountyFireManagementContext())
            {
                return ef.Role.OrderBy(o => o.RoleId).ToList();
            }
        }

        public int AddPosition(Position info)
        {
            using (CountyFireManagementContext ef = new CountyFireManagementContext())
            {
                if (!info.Sort.HasValue)
                {
                    var maxSortInfo = ef.Position.OrderByDescending(o => o.Sort).FirstOrDefault();
                    if (maxSortInfo != null)
                    {
                        info.Sort = maxSortInfo.Sort.Value + 1;
                    }
                    else
                    {
                        info.Sort = 1;
                    }
                }

                int isAdd = ef.Position.Where(p => p.PositionName == info.PositionName).ToList().Count();
                if (isAdd != 0)
                {
                    return -2;
                }

                ef.Position.Add(info);
                ef.Entry<Position>(info).State = EntityState.Added;
                return ef.SaveChanges();
            }
        }

        public int UpdatePosition(Position info)
        {
            using (CountyFireManagementContext ef = new CountyFireManagementContext())
            {
                var exist = ef.Position.Where(p => p.PositionId == info.PositionId).FirstOrDefault();
                if (exist == null)
                {
                    return -1;
                }
                else
                {
                    int isUpdate = ef.Position.Where(p => p.PositionId != info.PositionId && p.PositionName == info.PositionName).ToList().Count();
                    if (isUpdate != 0)
                    {
                        return -2;
                    }
                    exist.PositionName = info.PositionName;
                    exist.Sort = info.Sort;
                    ef.Position.Add(exist);
                    ef.Entry<Position>(exist).State = EntityState.Modified;
                    return ef.SaveChanges();
                }
            }
        }

        public ResultDto<dynamic, dynamic> GetPositionByPage(RequestPageBase page)
        {
            ResultDto<dynamic, dynamic> result = new ResultDto<dynamic, dynamic>();
            var sql = AppSetting.GetConfig("Sql:BasicSelect");
            sql = string.Format(sql, "position");
            using (CountyFireManagementContext ef = new CountyFireManagementContext())
            {
                string paramsSql = string.Empty;
                if (!string.IsNullOrEmpty(page.Keyword))
                {
                    paramsSql += page.Keyword.KeywordSql("PositionName");
                }
                paramsSql += page.OrderByPattern == 0 ? "Sort".OrderByDescending() : "Sort".OrderBy();
                paramsSql += page.Limit();
                sql += paramsSql;
                var countSql = sql.CountSql();
                var list = ef.Database.SqlQuery<Position>(sql);
                result.FirstParam = list;
                result.SecondParam = ef.Database.SqlQuery(countSql).Rows[0][0];
            }
            return result;
        }

        public int AddRolePower(Rolepower info)
        {
            using (CountyFireManagementContext ef = new CountyFireManagementContext())
            {
                int isAdd = ef.Rolepower.Where(p => p.RoleId == info.RoleId && p.PowerId==info.PowerId).ToList().Count();
                if (isAdd != 0)
                {
                    return -2;
                }
                ef.Rolepower.Add(info);
                ef.Entry<Rolepower>(info).State = EntityState.Added;
                return ef.SaveChanges();
            }
        }

        public Userinfo Get(int userId)
        {
            using (CountyFireManagementContext ef = new CountyFireManagementContext())
            {
                return ef.Userinfo.Where(p => p.UserId == userId).FirstOrDefault();
            }
        }

        public List<dynamic> GetRolePower(int roleId)
        {
            using (CountyFireManagementContext ef = new CountyFireManagementContext())
            {
                var linq = from a in ef.Rolepower select a;
                if (roleId > 0)
                {
                    linq = linq.Where(p => p.RoleId == roleId);
                }
                var list = (from a in linq.OrderBy(o => o.RoleId).ToList()
                            join b in ef.Role on a.RoleId equals b.RoleId
                            into c
                            let d = c.FirstOrDefault()
                            join e in ef.Power on a.PowerId equals e.PowerId
                            into f
                            let g = f.FirstOrDefault()
                            select new
                            {
                                a.RolePowerId,
                                a.PowerId,
                                a.RoleId,
                                a.CreateTime,
                                d.RoleName,
                                g.PowerName,
                                g.PowerKey
                            }).ToList<dynamic>();
                return list;
            }
        }

        public int AddPower(Power info)
        {
            using (CountyFireManagementContext ef = new CountyFireManagementContext())
            {
                if (!info.Sort.HasValue)
                {
                    var maxSortInfo = ef.Power.OrderByDescending(o => o.Sort).FirstOrDefault();
                    if (maxSortInfo != null)
                    {
                        info.Sort = maxSortInfo.Sort.Value + 1;
                    }
                    else
                    {
                        info.Sort = 1;
                    }
                }
                int isAdd = ef.Power.Where(p => p.PowerKey == info.PowerKey).ToList().Count();
                if (isAdd != 0)
                {
                    return -2;
                }
                ef.Power.Add(info);
                ef.Entry<Power>(info).State = EntityState.Added;
                return ef.SaveChanges();
            }
        }

        public List<Power> GetPower()
        {
            using (CountyFireManagementContext ef = new CountyFireManagementContext())
            {
                var linq = from a in ef.Power select a;
                return linq.OrderBy(o => o.Sort).ToList();
            }
        }

        public List<Rolepower> GetPowerByPage(RequestPageBase page)
        {
            using (CountyFireManagementContext ef = new CountyFireManagementContext())
            {
                var linq = from a in ef.Rolepower select a;
                
                return linq.OrderBy(o => o.RoleId).ToList();
            }
        }

        public int Add(Userinfo info)
        {
            using (CountyFireManagementContext ef = new CountyFireManagementContext())
            {
                if (info.Sort >0)
                {
                    var maxSortInfo = ef.Userinfo.OrderByDescending(o => o.Sort).FirstOrDefault();
                    if (maxSortInfo != null)
                    {
                        info.Sort = maxSortInfo.Sort + 1;
                    }
                    else
                    {
                        info.Sort = 1;
                    }
                }
                int isAdd = ef.Userinfo.Where(p => p.AccountName == info.AccountName).ToList().Count();
                if (isAdd != 0)
                {
                    return -2;
                }

                ef.Userinfo.Add(info);
                ef.Entry<Userinfo>(info).State = EntityState.Added;
                return ef.SaveChanges();
            }
        }

        public int Update(Userinfo info)
        {
            using (CountyFireManagementContext ef = new CountyFireManagementContext())
            {
                var exist = ef.Userinfo.Where(p => p.UserId == info.UserId).FirstOrDefault();
                if (exist == null)
                {
                    return -1;
                }
                else
                {

                    int isUpdate = ef.Userinfo.Where(p => p.UserId != info.UserId && p.AccountName == info.AccountName).ToList().Count();
                    if (isUpdate != 0)
                    {
                        return -2;
                    }
                    exist.UserId = info.UserId;
                    exist.Password = string.IsNullOrEmpty(info.Password) ? exist.Password : info.Password;
                    exist.Phone = info.Phone;
                    exist.RealName = info.RealName;
                    exist.DepId = info.DepId;
                    exist.PositionId = info.PositionId == 0 ? exist.PositionId : info.PositionId;
                    exist.RoleId = info.RoleId == 0 ? exist.RoleId : info.RoleId;
                    exist.Sort = info.Sort==0?exist.Sort:info.Sort;
                    exist.AddressCounty = info.AddressCounty;
                    exist.AddressTown = info.AddressTown;
                    exist.AddressVillage = info.AddressVillage;
                    ef.Userinfo.Add(exist);
                    ef.Entry<Userinfo>(exist).State = EntityState.Modified;
                    return ef.SaveChanges();
                }
            }
        }

        public int Remove(int userId)
        {
            using (CountyFireManagementContext ef = new CountyFireManagementContext())
            {
                var exist = ef.Userinfo.Where(p => p.UserId == userId).FirstOrDefault();

                var fireRel = ef.Fireinfo.Where(p => p.UserId == userId).FirstOrDefault();
                if (exist != null)
                {
                    ef.Userinfo.Add(exist);
                    ef.Entry<Userinfo>(exist).State = EntityState.Deleted;
                    return ef.SaveChanges();
                }
                else
                {
                    return -1;
                }
            }
        }

        public ResultDto<dynamic, dynamic> GetUserByPage(string addressCounty, string addressTown, string addressVillage, RequestPageBase page)
        {
            ResultDto<dynamic, dynamic> result = new ResultDto<dynamic, dynamic>();
            var sql = AppSetting.GetConfig("Sql:UserBLL.GetUserByPage");
            using (CountyFireManagementContext ef = new CountyFireManagementContext())
            {
                string paramsSql = string.Empty;
                if (!string.IsNullOrEmpty(page.Keyword))
                {
                    paramsSql += $" a.AccountName like '%{page.Keyword}%' ";
                    paramsSql += $" or a.RealName like '%{page.Keyword}%' ";
                    paramsSql += $" or f.DepName like '%{page.Keyword}%' ";
                    paramsSql += $" or b.PositionName like '%{page.Keyword}%' ";
                    paramsSql = $" and ({paramsSql})";
                }

                if(!string.IsNullOrEmpty(addressCounty))
                {
                    paramsSql += $" and c.LinchpinID ='{addressCounty}' ";
                }

                if (!string.IsNullOrEmpty(addressTown))
                {
                    paramsSql += $" and d.LinchpinID ='{addressTown}' ";
                }

                if (!string.IsNullOrEmpty(addressVillage))
                {
                    paramsSql += $" and e.LinchpinID ='{addressVillage}' ";
                }

                paramsSql += page.OrderByPattern == 0 ? "b.Sort".OrderByDescending() : "b.Sort".OrderBy();
                paramsSql += page.Limit();
                sql= string.Format(sql, paramsSql);
                var countSql = sql.CountSql();
                var list = ef.Database.SqlQuery<UserInfoModel>(sql);
                list.ForEach(f=>f.Password=string.Empty);
                result.FirstParam = list;
                result.SecondParam = ef.Database.SqlQuery(countSql).Rows[0][0];


            }
            return result;
        }


        public List<Rolelevel> GetRoleLevel() 
        {
            using (CountyFireManagementContext ef = new CountyFireManagementContext())
            {
                return ef.Rolelevel.OrderBy(o => o.RoleLevelId).ToList();
            }
        }
    }
}
